package com.wys.ws_im.service;

import com.wys.ws_im.dao.AccountDao;
import com.wys.ws_im.dao.OfflineMessageDao;
import com.wys.ws_im.dao.UserListDao;
import com.wys.ws_im.entity.Account;
import com.wys.ws_im.entity.OfflineMessage;
import com.wys.ws_im.entity.UserList;
import com.wys.ws_im.exception.FrameErrorCodeEnums;
import com.wys.ws_im.exception.FrameRuntimeException;
import com.wys.ws_im.vo.ServiceListVO;
import com.wys.ws_im.vo.UserListVO;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.core.db.KeyHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;


/**
 * 聊天服务
 *
 * @author wys
 * created in 21:32 2019/4/21
 */
@Slf4j
@Service
public class ChatService {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private UserListDao userListDao;

    @Autowired
    private OfflineMessageDao offlineMessageDao;

    /**
     * 用户获取聊天列表
     * @param aid
     * @return
     */
    public List<ServiceListVO> getServiceCMList(Integer aid) {

        if (aid == null) {
            throw new FrameRuntimeException(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }
        List<ServiceListVO> list = userListDao.getServiceList(aid);

        return list;
    }

    /**
     * 客服获取聊天列表
     * @param serviceId
     * @return
     */
    public List<UserListVO> getUserCMList(Integer serviceId) {

        if (serviceId == null) {
            throw new FrameRuntimeException(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }
        List<UserListVO> list = userListDao.getUserList(serviceId);

        return list;
    }

    /**
     * 选择客服，增加聊天客服
     * 如果是新的客服，就增加到客服聊天列表中
     * 如果不是，就获取最近三十天内的联系人
     *
     * @param aid
     * @param serviceId
     * @return
     */
    public List<ServiceListVO> addService(Integer aid, Integer serviceId) {
        log.info("{} {}", aid, serviceId);
        if (aid == null || serviceId == null) {
            throw new FrameRuntimeException(FrameErrorCodeEnums.BIND_ARGS_ERROR);
        }

        Account user = accountDao.createLambdaQuery().andEq(Account::getAid, aid).single();
        Account service = accountDao.createLambdaQuery().andEq(Account::getAid, serviceId).single();
        if (user == null || service == null) {
            throw new FrameRuntimeException(FrameErrorCodeEnums.ACCOUNT_NULL_ERROR);
        }

        UserList userList = userListDao.createLambdaQuery().andEq(UserList::getAid, aid)
                .andEq(UserList::getServiceId, serviceId)
                .single();
        // 空可以添加
        // 增加离线消息 == > 增加对应客服 和 用户 的离线消息实体
        if (userList == null) {
            UserList newUserList = new UserList();
            newUserList.setAid(aid);
            newUserList.setServiceId(serviceId);
            newUserList.setCreatedTime(new Date());
            newUserList.setUpdatedTime(new Date());
//            userListDao.insertTemplate(newUserList);
            KeyHolder keyHolder = userListDao.insertReturnKey(newUserList);


            // 客服的离线消息
            OfflineMessage offlineMessage = new OfflineMessage();
            offlineMessage.setUlid(keyHolder.getInt());
            offlineMessage.setAid(serviceId);
            offlineMessage.setCount(0);
            offlineMessageDao.insert(offlineMessage);

            log.info("初始化 离线消息  116 aid{}  servid {}", aid, serviceId);

            // 用户的离线消息
            OfflineMessage offlineMessage1 = new OfflineMessage();
            offlineMessage1.setUlid(keyHolder.getInt());
            offlineMessage1.setAid(aid);
            offlineMessage1.setCount(0);
            offlineMessageDao.insert(offlineMessage1);

        }
        // 不空 改下选择的那个客服的更新时间，这样就会排在前面
        else {
            userList.setUpdatedTime(new Date());
            userListDao.updateById(userList);
        }

        List<ServiceListVO> list = userListDao.getServiceList(aid);
        log.info("list {}", list);

        return list;
    }


}
